
package com.probiz.estore.customer.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.probiz.estore.common.model.customer.Membership;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.customer.dao.MembershipDao;

/**
 * Membership Data Access Object (DAO) implementation. Developer introduced
 * interfaces should be declared here. Won't get overwritten.
 */
public class MembershipDaoImpl extends HibernateGenericDaoImpl<Membership>
		implements MembershipDao {
	@Override
	protected void initFilterHqlMap() {
		filterHqlMap.put(BaseObject.FilterHqlName.DEFAULT, "from Membership m order by m.membershipLevel desc");
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.MembershipDao#getLowerLevelMembership(java.lang.Integer)
	 */
	public Membership getLowerLevelMembership(Integer level){
		List<Membership> list = this.findByHql("from Membership m where m.membershipLevel < ? order by m.membershipLevel desc", level);
		if(list.size()>0){
			return list.get(0);
		}else{
			list= this.listByProperty("membershipLevel", Membership.MEMBERSHIP_LEVEL_BASE);
			return list.get(0);
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.MembershipDao#getHigherLevelMembership(java.lang.Integer)
	 */
	public Membership getHigherLevelMembership(Integer level){
		List<Membership> list = this.findByHql("from Membership m where m.membershipLevel >? order by m.membershipLevel asc", level);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null; //确实没有返回null
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.MembershipDao#isLevelExist(java.lang.Integer, java.lang.Integer)
	 */
	public boolean isLevelExist(Integer membershipId, Integer level) {
		String hql = "";
		Object[] params = null;
		if (membershipId == null) {
			hql = "select count(*) from com.probiz.estore.common.model.customer.Membership vo where vo.membershipLevel=?";
			params = new Object[] { level };
		} else {
			hql = "select count(*) from com.probiz.estore.common.model.customer.Membership vo where vo.membershipLevel=? and vo.membershipId<>?";
			params = new Object[] { level, membershipId };
		}
		Object reValue = findUnique(hql, params);
		if (reValue == null)
			return false;

		int rows = new Integer(reValue.toString()).intValue();
		if (rows > 0)
			return true;
		else
			return false;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.MembershipDao#getAllMembershipsOrderByLevel(boolean)
	 */
	public List<Membership> getAllMembershipsOrderByLevel(boolean isAsc){
		return createCriteria("membershipLevel", isAsc).list();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.MembershipDao#getAllMembershipsOrderByLevelExcluedOne(boolean, java.lang.Integer)
	 */
	public List<Membership> getAllMembershipsOrderByLevelExcluedOne(boolean isAsc,Integer membershipId){
		if(null == membershipId){
			return getAllMembershipsOrderByLevel(isAsc);
		}else{
			return createCriteria("membershipLevel", isAsc,Restrictions.ne("membershipId", membershipId)).list();
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.MembershipDao#doUpdateCustomerMembership(java.lang.Integer, java.lang.Integer)
	 */
	public void doUpdateCustomerMembership(final Integer preMembershipId,final Integer postMembershipId){
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Connection con=session.connection(); 
				PreparedStatement stmt=con.prepareStatement("update app_user u set u.membershipId=? where u.membershipId = ?");
				stmt.setObject(1, postMembershipId);
				stmt.setObject(2, preMembershipId);
				stmt.executeUpdate(); 
				return null;
			}

		});
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.MembershipDao#getMaxMemberLevelAndUpgradeShopPoint()
	 */
	public Map getMaxMemberLevelAndUpgradeShopPoint() {
		List list = this.findByHql("select max(m.membershipLevel) membershipLevel ,max(m.upgradeShopPoint) upgradeShopPoint from Membership m");
		if(list.size()>0){
			return (Map)list.get(0);
		}else{
			return null;
		}

	}

}